package com.jinmdz.fmis.api.controller;

import com.jinmdz.fmis.common.ext.StringBuilderExt;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.constant.Constants;
import com.jinmdz.fmis.core.constant.EResultCode;
import com.jinmdz.fmis.core.wrapper.ResultWrapper;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author LiCongLu
 * @date 2019-12-24 16:25
 */
@ApiIgnore
@RestController
public class GlobalController implements ErrorController {

    /**
     * 默认接口
     *
     * @return
     */
    @RequestMapping(value = "", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResult root() {
        return rootA();
    }

    /**
     * 默认/接口
     *
     * @return
     */
    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResult rootA() {
        return ResultWrapper.getResult().setMessage("已启动接口");
    }

    /**
     * 错误页面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = Constants.ERROR_PATH, method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResult error(HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = ResultWrapper.getResult(response.getStatus());
        StringBuilderExt builder = new StringBuilderExt();
        builder.formatLine("{0};status:{1}", result.getMessage(), String.valueOf(response.getStatus()));
        return result.setCode(EResultCode.CODE0.getCode()).setMessage(builder.toString());
    }

    @Override
    public String getErrorPath() {
        return Constants.ERROR_PATH;
    }
}